What is difference between pass by value and pass by reference?
pass by value and reference
1073
09-Jul-2018
Prakash nidhi Verma
10-Jul-2018Pass by value :
In this we pass copy of actual variables in function as a parameter. Hence any modification on parameters inside the function will not reflect in the actual variable.
example:
#include<stdio.h> int main(){ int a=5,b=10; swap(a,b); printf("%d%d",a,b); return 0; } void swap(int a,int b){ int temp; temp =a; a=b; b=temp; }Pass by reference :
In this we pass memory address by a reference its actual variables in function as a parameter. Hence, any modification on parameters inside the function will reflect in the actual variable.
#incude<stdio.h> int main(){ int a=5,b=10; swap(&a,&b); printf("%d %d",a,b); return 0; } void swap(int *a,int *b){ int *temp; *temp =*a; *a=*b; *b=*temp; }